﻿using ClusterLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormExample
{
    public partial class Form1 : Form
    {
        enum Action
        {
            ADD_CLUSTER,
            ADD_SUBCLASS,
            MOVE_CLUSTER,
            MOVE_SUBCLASS,
            REMOVE_CLUSTER,
            REMOVE_SUBCLASS,
            NONE
        }


        private Map clusterMap;
        private Action action;

        private int startX, endX;
        private int startY, endY;

        private ClusterView activeCluster;
        private ClusterView activeSubclass;

        private int radius;
        public Form1()
        {
            InitializeComponent();
            this.clusterMap = new Map();
            this.action = Action.NONE;

            this.radius = 20;
        }

        private void addClusterButton_Click(object sender, EventArgs e)
        {
            this.action = Action.ADD_CLUSTER;
        }

        private void clusterMapPictureBox_MouseClick(object sender, MouseEventArgs e)
        {

        }

        private void drawMap()
        {
            Graphics g = this.clusterMapPictureBox.CreateGraphics();
            g.Clear(this.clusterMapPictureBox.BackColor);

            foreach (ClusterView clusterView in this.clusterMap.Clusters)
            {
                clusterView.drawCluster(g);
            }
            if (this.activeSubclass != null)
            {
                this.activeSubclass.drawCluster(g);
            }
        }

        private void addSubclassButton_Click(object sender, EventArgs e)
        {
            this.action = Action.ADD_SUBCLASS;
        }

        private void moveClusterButton_Click(object sender, EventArgs e)
        {
            this.action = Action.MOVE_CLUSTER;
        }

        private void clusterMapPictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            switch (this.action)
            {
                case Action.ADD_CLUSTER:
                    this.clusterMap.addCluster(new CircleClusterView(new Cluster(), new Circle(e.X, e.Y, this.radius)));
                    break;
                case Action.ADD_SUBCLASS:
                    this.clusterMap.addSubclass(e.X, e.Y);
                    break;
                case Action.MOVE_CLUSTER:
                    this.startX = e.X;
                    this.startY = e.Y;
                    this.activeCluster = this.clusterMap.getCluster(e.X, e.Y);
                    break;
                case Action.MOVE_SUBCLASS:
                    this.startX = e.X;
                    this.startY = e.Y;
                    this.activeCluster = this.clusterMap.getCluster(e.X, e.Y);
                    if (this.activeCluster != null)
                    {
                        this.activeSubclass = this.activeCluster.getSubclass(e.X, e.Y);
                        if (this.activeSubclass != null)
                        {
                            this.activeCluster.removeSubclass(this.activeSubclass);
                        }
                    }
                    break;
                case Action.REMOVE_CLUSTER:
                    this.clusterMap.removeCluster(this.clusterMap.getCluster(e.X, e.Y));
                    break;
                case Action.REMOVE_SUBCLASS:
                    this.clusterMap.removeSubclass(e.X, e.Y);
                    break;
                case Action.NONE:
                    break;
            }

            this.drawMap();
        }

        private void clusterMapPictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            switch (this.action)
            {
                case Action.MOVE_CLUSTER:
                    if (this.activeCluster != null)
                    {
                        this.endX = e.X;
                        this.endY = e.Y;
                        this.activeCluster.moveCluster(this.endX - this.startX, this.endY - this.startY);
                        this.startX = this.endX;
                        this.startY = this.endY;
                    }
                    break;
                case Action.MOVE_SUBCLASS:
                    if (this.activeSubclass != null)
                    {
                        this.endX = e.X;
                        this.endY = e.Y;
                        this.activeSubclass.moveCluster(this.endX - this.startX, this.endY - this.startY);
                        this.startX = this.endX;
                        this.startY = this.endY;
                    }
                    break;
            }

            this.drawMap();

        }

        private void clusterMapPictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            switch (this.action)
            {
                case Action.MOVE_CLUSTER:
                    this.activeCluster = null;
                    break;
                case Action.MOVE_SUBCLASS:
                    if (this.activeSubclass != null)
                    {
                        ClusterView destinationCluster = this.clusterMap.getCluster(e.X, e.Y);
                        if (destinationCluster != null)
                        {
                            destinationCluster.addSubclass(this.activeSubclass);
                        }
                        else
                        {
                            this.activeCluster.addSubclass(this.activeSubclass);
                        }
                    }
                    this.activeCluster = null;
                    this.activeSubclass = null;
                    break;
            }
        }

        private void moveSubclassButton_Click(object sender, EventArgs e)
        {
            this.action = Action.MOVE_SUBCLASS;
        }

        private void removeClusterButton_Click(object sender, EventArgs e)
        {
            this.action = Action.REMOVE_CLUSTER;
        }

        private void removeSubclassButton_Click(object sender, EventArgs e)
        {
            this.action = Action.REMOVE_SUBCLASS;
        }
    }
}
